package top.mccat.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.mccat.common.core.domain.bk.BkProduct;
import top.mccat.common.mapper.bk.BkProductMapper;
import top.mccat.common.mapper.bk.BkProductSupplierMapper;
import top.mccat.common.service.IBkProductService;

import java.util.List;

/**
 * 产品Service业务层处理
 * 
 * @author raven
 * @date 2022-11-06
 */
@Service
public class BkProductServiceImpl implements IBkProductService
{
    @Autowired
    private BkProductMapper bkProductMapper;
    @Autowired
    private BkProductSupplierMapper bkProductSupplierMapper;

    /**
     * 查询产品
     * 
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public BkProduct selectBkProductById(Integer id)
    {
        return bkProductMapper.selectBkProductById(id);
    }

    /**
     * 查询产品列表
     * 
     * @param bkProduct 产品
     * @return 产品
     */
    @Override
    public List<BkProduct> selectBkProductList(BkProduct bkProduct)
    {
        return bkProductMapper.selectBkProductList(bkProduct);
    }

    @Override
    public List<BkProduct> selectBkProductNameList(BkProduct bkProduct) {
        return bkProductMapper.selectBkProductNameList(bkProduct);
    }

    @Override
    public List<BkProduct> selectBkProductModelList(String productName) {
        return bkProductMapper.selectBkProductModelList(productName);
    }

    /**
     * 新增产品
     * 
     * @param bkProduct 产品
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBkProduct(BkProduct bkProduct)
    {
        int row = bkProductMapper.insertBkProduct(bkProduct);
        if(bkProduct.getSupplierId() != null){
            bkProductSupplierMapper.insertBkProductSupplier(bkProduct);
        }
        return row;
    }

    /**
     * 修改产品
     * 
     * @param bkProduct 产品
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBkProduct(BkProduct bkProduct)
    {
        int row = bkProductMapper.updateBkProduct(bkProduct);
        if(bkProduct.getSupplierId() != null) {
            //删除
            bkProductSupplierMapper.deleteBkProductSupplierByProductId(bkProduct.getId().longValue());
            //新增
            bkProductSupplierMapper.insertBkProductSupplier(bkProduct);
        }
        //如果父id为0则需要修改子类参数，由于商品类别无参数，故可以为null
        if(bkProduct.getParentId() == null){
            bkProductMapper.updateBkProductEnable(bkProduct);
        }
        return row;
    }

    /**
     * 批量删除产品
     * 
     * @param ids 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteBkProductByIds(Integer[] ids)
    {
        return bkProductMapper.deleteBkProductByIds(ids);
    }

    /**
     * 删除产品信息
     * 
     * @param id 产品主键
     * @return 结果
     */
    @Override
    public int deleteBkProductById(Integer id)
    {
        return bkProductMapper.deleteBkProductById(id);
    }
}
